package com.zrwl.poscloud.commons.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘思童
 * @create 2022-09-14 13:56
 **/
public class BuildTreeFromList {
    public static JSONArray getTreeFromList(List dataList) {
        JSONArray dataArray = JSON.parseArray(JSON.toJSONString(dataList));

        JSONArray rootList = new JSONArray();

        JSONObject nowHouse = null, nowWindow = null;
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject entity = dataArray.getJSONObject(i);

            if (nowHouse == null || !nowHouse.getString("value").equals(entity.getString("houseUid"))) {
                nowHouse = new JSONObject();
                nowHouse.put("text", entity.getString("houseName")+'-'+entity.getString("houseCode"));
                nowHouse.put("value", entity.getString("houseUid"));
                nowHouse.put("type",1);
                rootList.add(nowHouse);

                JSONArray windowList = new JSONArray();
                nowWindow = new JSONObject();
                nowWindow.put("text", entity.getString("windowName"));
                nowWindow.put("value", entity.getString("windowUid"));
                nowWindow.put("houseUid",entity.getString("houseUid"));
                nowWindow.put("houseName",entity.getString("houseName"));
                nowWindow.put("type",2);
                nowHouse.put("childrenList", windowList);

                windowList.add(nowWindow);

                JSONArray posList = new JSONArray();
                nowWindow.put("childrenList", posList);
                entity.put("text", entity.getString("name"));
                entity.put("value", entity.getString("uid"));
                entity.put("houseUid",entity.getString("houseUid"));
                entity.put("houseName",entity.getString("houseName"));
                entity.put("windowName", entity.getString("windowName"));
                entity.put("windowUid", entity.getString("windowUid"));
                entity.put("type",3);
                posList.add(entity);

            } else {

                if (nowWindow.getString("value").equals(entity.getString("windowUid"))) {
                    entity.put("text", entity.getString("name"));
                    entity.put("value", entity.getString("uid"));
                    entity.put("houseUid",entity.getString("houseUid"));
                    entity.put("houseName",entity.getString("houseName"));
                    entity.put("windowName", entity.getString("windowName"));
                    entity.put("windowUid", entity.getString("windowUid"));
                    entity.put("type",3);
                    nowWindow.getJSONArray("childrenList").add(entity);
                } else {
                    nowWindow = new JSONObject();
                    nowWindow.put("text", entity.getString("windowName"));
                    nowWindow.put("value", entity.getString("windowUid"));
                    nowWindow.put("houseUid",entity.getString("houseUid"));
                    nowWindow.put("houseName",entity.getString("houseName"));
                    nowWindow.put("type",2);
                    nowHouse.getJSONArray("childrenList").add(nowWindow);

                    JSONArray posList = new JSONArray();
                    nowWindow.put("childrenList", posList);
                    entity.put("text", entity.getString("name"));
                    entity.put("value", entity.getString("uid"));
                    entity.put("houseUid",entity.getString("houseUid"));
                    entity.put("houseName",entity.getString("houseName"));
                    entity.put("windowName", entity.getString("windowName"));
                    entity.put("windowUid", entity.getString("windowUid"));
                    entity.put("type",3);
                    posList.add(entity);
                }
            }

        }
        return rootList;
    }


    /***
     * 乱序下任然未完善
     * @param dataList
     * @return
     */
    public static JSONArray getTreeFromList2(List dataList) {
        JSONArray dataArray = JSON.parseArray(JSON.toJSONString(dataList));

        JSONArray rootList = new JSONArray();
        JSONObject root = new JSONObject();
        rootList.add(root);
        root.put("id", "0");
        root.put("name", "根节点");
        root.put("childrenList", new ArrayList());

        Map<String, Map> indexOfSuper = new HashMap();
        indexOfSuper.put("0", root);

        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject entity = dataArray.getJSONObject(i);
            Map superMap = indexOfSuper.get(entity.getString("superId"));
            if (superMap == null) {
                superMap = new HashMap();
                superMap.put("childrenList", new ArrayList());
                indexOfSuper.put(entity.getString("superId"), superMap);
            }
            ((List) superMap.get("childrenList")).add(entity);

            Map me = indexOfSuper.get(entity.getString("id"));
            if (me == null) {
                indexOfSuper.put(entity.getString("id"), entity);
                entity.put("childrenList", new ArrayList());
            } else {
                me.put("name", entity.get("name"));
                me.put("superId", entity.get("superId"));
//                BeanUtils.copyProperties(entity, me);
                Map superSuperMap = indexOfSuper.get(entity.getString("superId"));
                if (superSuperMap == null) {
                    superSuperMap = new HashMap();
                    superSuperMap.put("childrenList", new ArrayList());
                    indexOfSuper.put(entity.getString("superId"), superSuperMap);
                }
                ((List) superSuperMap.get("childrenList")).add(me);

            }

        }
        return rootList;
    }


    @Test
    public void tt() {
        String jsonDataStr = "[{" +
                "  house: \"A\", window: \"a\", pos: \"1\"" +
                "}, {" +
                "  house: \"A\", window: \"a\", pos: \"2\"" +
                "}, {" +
                "  house: \"A\", window: \"b\", pos: \"3\"" +
                "}, {" +
                "  house: \"A\", window: \"b\", pos: \"4\"" +
                "}, {" +
                "  house: \"B\", window: \"a\", pos: \"1\"" +
                "}, {" +
                "  house: \"B\", window: \"a\", pos: \"2\"" +
                "}, {" +
                "  house: \"B\", window: \"b\", pos: \"3\"" +
                "}, {" +
                "  house: \"B\", window: \"b\", pos: \"4\"" +
                "}]";
        JSONArray jsonArray = JSONArray.parseArray(jsonDataStr);
        List dataList = new ArrayList();
        for (int i = 0; i < jsonArray.size(); i++) {
            dataList.add(jsonArray.get(i));
        }
        JSONArray result = getTreeFromList(dataList);
//        System.out.println(result.toJSONString());


        String jsonDataStr2 = " [{" +
                "  id: 6," +
                "  name: \"A\"," +
                "  superId: 0" +
                "}, {" +
                "  id: 2," +
                "  name: \"B\"," +
                "  superId: 0" +
                "}, {" +
                "  id: 3," +
                "  name: \"C\"," +
                "  superId: 1" +
                "}, {" +
                "  id: 4," +
                "  name: \"D\"," +
                "  superId: 3" +
                "}, {" +
                "  id: 5," +
                "  name: \"E\"," +
                "  superId: 2" +
                "}, {" +
                "  id: 1," +
                "  name: \"F\"," +
                "  superId: 2" +
                "}]";
        JSONArray jsonArray2 = JSONArray.parseArray(jsonDataStr2);
        List dataList2 = new ArrayList();
        for (int i = 0; i < jsonArray2.size(); i++) {
            dataList2.add(jsonArray2.get(i));
        }
        JSONArray result2 = getTreeFromList2(dataList2);
        System.out.println(result2.toJSONString());
    }
}
